
//  StatusViewModel.swift
//  ShangHaiWeiBo
//
//  Created by 蔡杰 on 16/3/1.
//  Copyright © 2016年 蔡杰. All rights reserved.
//

import UIKit

struct StatusCellLayout {
    /// 控件距离
    let margin: CGFloat = 12

    /// 头像大小
    let iconSize = CGSize(width: 36, height: 36)
    /// 工具栏高度
    let toolbarHeight:CGFloat = 36
    /// 配图的间距
    let picturesMargin:CGFloat = 2
    /// 配图的行个数
    let picturesPerRow:CGFloat = 3
    /// 配图视图的宽度
    let picturesViewWidth:CGFloat
    /// 计算图片大小
    let pictureSize:CGSize

    init(){
    
        picturesViewWidth = UIScreen.mainScreen().bounds.width - 2 * margin
        let pictureWH = (picturesViewWidth - (picturesPerRow - 1) * picturesMargin) / picturesPerRow
        pictureSize = CGSize(width: pictureWH, height: pictureWH)
    }
    
}

let CZStatusCellLayout = StatusCellLayout()

class StatusViewModel {
    
    var status: Status
    
    /// 会员图标
    var userMemberImage: UIImage?
    /// 认证图标
    var userVipImage: UIImage?
    // MARK: - 微博数据
    /// 来源字符串
    var sourceText: String?
    /// 创建日期
    var createTime: NSDate?

    //原创微博配图大小
    var picturesViewSize = CGSizeZero
    //被转发微博配图大小
    var retweetedPicturesViewSize = CGSizeZero
    //被转发微博字符串拼接
    var retweetedText: String?{
        if status.retweeted_status == nil{
        
            return nil
        }
        
        return "@" + (status.retweeted_status?.user?.screen_name ?? "") +
        ":" + (status.retweeted_status?.text ?? "")
        
    }
    // MARK: - 构造函数
    /// 使用微博模型实例化视图模型
    ///
    /// - parameter status: 微博模型
    ///
    /// - returns: 视图模型
    
    
    init(status: Status){
    
        self.status = status
        
        createUserData()
        createStatusData()
        
        picturesViewSize = calcPicturesViewSize(status.pic_urls)
        
        retweetedPicturesViewSize = calcPicturesViewSize(status.retweeted_status?.pic_urls)
        
    }
    
    private func calcPicturesViewSize(pic_urls: [StatusPictures]?) ->CGSize{
    
        let count = pic_urls?.count ?? 0
        if count == 0 {
        
            return CGSizeZero
        }
        let layout = CZStatusCellLayout
        
        let row = CGFloat((count - 1) / Int(layout.picturesPerRow) + 1)
        
        let heigth = row * layout.pictureSize.height + (row - 1) * layout.picturesMargin
        return CGSize(width: layout.picturesViewWidth, height: heigth)
        
    }
    private func createStatusData (){
    
        if let source = status.source,
        startIndex = source.rangeOfString("\">")?.endIndex,
            endIndex = source.rangeOfString("</a>")?.startIndex{
        
                sourceText = "来自 " + source.substringWithRange(startIndex..<endIndex)
        }
        createTime = NSDate.cj_sinaDate(status.created_at)
        
    }
    private func createUserData() {
    
        
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            userMemberImage = UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        
        // 认证图标 -1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
        switch status.user?.verified_type ?? -1 {
        case 0: userVipImage = UIImage(named: "avatar_vip")
        case 2, 3, 5: userVipImage = UIImage(named: "avatar_enterprise_vip")
        case 220: userVipImage = UIImage(named: "avatar_grassroot")
        default: break
        }
        
    }
    
}